package cn.droidlover.xdroidmvp.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Created by Administrator on 2017/4/6.
 */

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> {
    protected final List<T> mData;
    protected final Context mContext;
    protected LayoutInflater mInflater;
    protected ListViewItemClickListener mClickListener;
    protected ListViewChildClickListener mChildClickListener;
    protected ListItemLongClickListener mLongClickListener;


    public BaseRecyclerAdapter(Context ctx) {
        mData = new ArrayList<T>();
        mContext = ctx;
        mInflater = LayoutInflater.from(ctx);
    }



    public void setLongClickListener(ListItemLongClickListener mLongClickListener) {
        this.mLongClickListener = mLongClickListener;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final RecyclerViewHolder holder = new RecyclerViewHolder(mContext,
                mInflater.inflate(getItemLayoutId(viewType), parent, false));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getLayoutPosition();
                if (mClickListener != null) {
                    mClickListener.itemClick(position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int position = holder.getLayoutPosition();
                if (mLongClickListener != null) {
                    mLongClickListener.itemLongClick(position);
                }

                return false;
            }
        });
        return holder;
    }


    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, int position) {

        bindData(holder, position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void add(int pos, T item) {
        mData.add(pos, item);
        notifyItemInserted(pos);
    }

    public void add(T item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void delete(int pos) {
        mData.remove(pos);
        notifyItemRemoved(pos);
    }

    public void setItemClickListener(ListViewItemClickListener itemClickListener) {
        mClickListener = itemClickListener;
    }

    public void setChildClickListener(ListViewChildClickListener mChildClickListener) {
        this.mChildClickListener = mChildClickListener;
    }

    public void addList(List<T> list) {
        this.mData.addAll(list);
        notifyDataSetChanged();
    }

    public void addList(boolean isRefresh, List<T> list) {
        if (isRefresh) {
            mData.clear();
        }
        if (list != null) {
            this.mData.addAll(list);
        }
        notifyDataSetChanged();
    }

    public List<T> getmList() {
        return mData;
    }


    public T getItemByPosition(int position) {
        return mData.get(position);
    }

    public void remove(List<T> datas) {
        mData.removeAll(datas);
        notifyDataSetChanged();
    }


    public void remove(T t) {
        if (null != t) {
            mData.remove(t);
            notifyDataSetChanged();
        }

    }

    public void removeAll() {
        mData.clear();
        notifyDataSetChanged();
    }

    public abstract int getItemLayoutId(int viewType);

//    public abstract void bindData(RecyclerViewHolder holder, int position, T item);

    public void bindData(RecyclerViewHolder holder, int position) {
    }


    /**
     * item移动后调用些方法更新
     *
     * @param fromPosition
     * @param toPosition
     */
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(mData, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }

    /**
     * 删除item调用此方法
     *
     * @param position
     */
    public void onItemRemove(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
    }

}